<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 第二行用tree树状结构组件 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true" @node-click="handleNodeClick">
          <template slot-scope="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog
          :title="isEdit?'编辑':'添加'"
          :visible.sync="dialogVisible"
          width="60%"
          :before-close="handleClose"
          @close="hDialogClose"
        >
          <!-- 弹框表单 -->
          <didalogFrom :id="curId" ref="dialog" :old-list="oldList" :is-edit="isEdit" @close="hSuccess" />
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
import didalogFrom from './didalogFrom.vue'
import { tranListToTreeData } from '@/utils/index'
import { getDepartments, deleteDepartments } from '@/api/department'
export default {
  components: {
    didalogFrom
  },
  data() {
    return {
      oldList: [],
      list: [], // 部门列表
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      curId: null,
      dialogVisible: false, // Dialog弹出框
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    async loadDepartments() { // 封装获取部门的请求
      const res = await getDepartments()
      // 调用封装的转换函数
      this.oldList = res.data.depts
      this.list = tranListToTreeData(res.data.depts).filter(item => item.pid !== '-1')
    },
    hSuccess() {
      this.dialogVisible = false
      this.$refs.dialog.delRule()
      this.loadDepartments()
    },
    hAdd(id) {
      this.isEdit = false // 是否处于编辑状态
      this.curId = id
      this.dialogVisible = true
    }, // 添加部门的点击函数
    hEdit(id) {
      this.isEdit = true // 是否处于编辑状态
      this.dialogVisible = true
      this.curId = id
      // 敲黑板!!!! 点击的时候组件还没有没创建
      this.$nextTick(function() {
        console.log(this.$refs.dialog)
        this.$refs.dialog.loadDetail()
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }, // 点击×关闭的弹窗
    hDel(id) { // 删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteDepartments(id)
        // 重新线软渲染
        this.loadDepartments()
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: res.message
          })
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hDialogClose() {
      console.log('close')
      this.$refs.dialog.delRule()
    }
  }
}
</script>
